<?php
# http://projects:8081/cdapp-2kt-api/api/flow_ntfm_load_airports_nrs.php

include_once '_portal.php';

const NTFM_API_URL_DNR = 'http://10.202.91.233:2334/remote/map/NTFM-MONIT/monitor/efficacy/queryDailyNormalRate';
const NTFM_API_URL_HNR = 'http://10.202.91.233:2334/remote/map/NTFM-MONIT/monitor/efficacy/queryHourNormalRate';
const NORMAL_RATE_ITEMS = ['ZUUU', 'ZUTF', 'ZUMY'];
const NORMAL_RATE_TYPES = ['flight', 'dep', 'tkf', 'arr', 'lnd', 'start', 'release'];

$output = isset($_GET['output']) ? $_GET['output'] : 0;

if ($output) {
  output(load_nrs());
  die();
}

function load_nrs() {
  $st = date('Y-m-d');
  $items = NORMAL_RATE_ITEMS;
  $types = NORMAL_RATE_TYPES;
  $results = [];
  foreach ($items as $item) {
    $results[$item] = [];
    $results[$item]['airport'] = $item;
    $dnr = load_dnr($item, $st);
    $results[$item]['dnr_flight'] = is_null($dnr) ? null : $dnr['normal_rate'];
    $results[$item]['dnr_dep'] = is_null($dnr) ? null : $dnr['normal_rate_dep'];
    $results[$item]['dnr_tkf'] = is_null($dnr) ? null : $dnr['normal_rate_tkf'];
    $results[$item]['dnr_arr'] = is_null($dnr) ? null : $dnr['normal_rate_arr'];
    $results[$item]['dnr_lnd'] = is_null($dnr) ? null : $dnr['normal_rate_lnd'];
    $results[$item]['dnr_start'] = is_null($dnr) ? null : $dnr['normal_rate_start'];
    $results[$item]['dnr_release'] = is_null($dnr) ? null : $dnr['normal_rate_release'];
    foreach ($types as $type) {
      $hnr = load_hnr($item, $st, $type);
      $hnr_current = 0;
      if (!is_null($hnr)) {
        $res = $hnr['normalRate'];
        for ($i = count($res) - 1; $i >= 0; $i--) {
          if ($res[$i] != null) {
            $hnr_current = $res[$i];
          }
        }
      }
      // $results[$item]['hnr_' . $type] = is_null($hnr) ? null : $hnr['normalRate'];
      $results[$item]['hnr_' . $type] = $hnr_current;
    }
  }
  return $results;
}

function load_dnr($item, $st) {
  $curl = curl_init();
  curl_setopt_array($curl, array(
    CURLOPT_URL => NTFM_API_URL_DNR,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => "item={$item}&st={$st}",
    CURLOPT_HTTPHEADER => array(
      'Content-Type: application/x-www-form-urlencoded',
    ),
  ));
  $response = curl_exec($curl);
  curl_close($curl);
  return parse_response($response);

}

function load_hnr($item, $st, $type) {
  $curl = curl_init();
  curl_setopt_array($curl, array(
    CURLOPT_URL => NTFM_API_URL_HNR,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => "item={$item}&st={$st}&type={$type}",
    CURLOPT_HTTPHEADER => array(
      'Content-Type: application/x-www-form-urlencoded',
    ),
  ));
  $response = curl_exec($curl);
  curl_close($curl);
  return parse_response($response);
}

function parse_response($response) {
  if ($response != null && $response != '' && $response) {
    $res_object = json_parse($response);
    if (gettype($res_object) == 'array') {
      return $res_object['data'];
    } else {
      return null;
    }
  } else {
    return null;
  }

}

function fetch_current_nrs() {

}